package com.eascs.afterloan.dao;

import org.springframework.stereotype.Repository;

import com.eascs.admin.common.dao.BaseO2oDao;
import com.eascs.afterloan.entity.O2oLoanapplyOverdue;
import com.eascs.afterloan.form.O2oLoanapplyOverdueForm;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.DateUtils;
import com.eascs.common.util.StringUtil;

@Repository
public class O2oLoanapplyOverdueDao extends BaseO2oDao {

	public O2oLoanapplyOverdue queryByLoanapplyNo(String loanapplyNo) {
		String sql = "select * from o2o_loanapply_overdue t where t.is_delete='N' and t.loanapply_no=?";
		return this.findEntity(sql, O2oLoanapplyOverdue.class, loanapplyNo);
	}

	public PageResult<Object[]> queryByPage(O2oLoanapplyOverdueForm form) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<Object[]>(Object[].class, form.getPageSize(), form.getPageIndex());
		StringBuffer querySql = new StringBuffer();
		querySql.append("select t.id,t.bill_code,t.province,t.loan_user_name,t.loan_operer,t.loan_bank_name,t.overdue_total,t.overdue_principal,t.overdue_interest,t.penalty_total,t.overdue_time from o2o_loanapply_overdue t where 1=1 and is_delete='N' ");
		if (StringUtil.isNotBlank(form.getBillCode())) {
			querySql.append(" and t.bill_code like ? ");
			sqlPageQuery.addLikeParam(form.getBillCode());
		}
		if (StringUtil.isNotBlank(form.getLoanUserName())) {
			querySql.append(" and t.loan_user_name like ? ");
			sqlPageQuery.addLikeParam(form.getLoanUserName());
		}
		if (StringUtil.isNotBlank(form.getProvince())) {
			querySql.append(" and t.province like ? ");
			sqlPageQuery.addLikeParam(form.getProvince());
		}
		if (StringUtil.isNotBlank(form.getLoanBankName())) {
			String sql = "SELECT item_name FROM pub_dditem where item_code=? and dd_no='10340'";
			String bankName = this.findString(sql, form.getLoanBankName());
			querySql.append(" and t.loan_bank_name like ? ");
			sqlPageQuery.addLikeParam(bankName);
		}
		if (StringUtil.isNotBlank(form.getLoanOperer())) {
			querySql.append(" and t.loan_operer like ? ");
			sqlPageQuery.addLikeParam(form.getLoanOperer());
		}
		if (StringUtil.isNotBlank(form.getOverdueTimeStart())) {
			querySql.append(" and t.overdue_time >= ? ");
			sqlPageQuery.addParam(form.getOverdueTimeStart());
		}
		if (StringUtil.isNotBlank(form.getOverdueTimeEnd())) {
			querySql.append(" and t.overdue_time < ? ");
			sqlPageQuery.addParam(DateUtils.formateStandDate(DateUtils.addDate(DateUtils.formateStandDate(form.getOverdueTimeEnd()), 1)));
		}
		querySql.append(" order by t.update_time asc");
		sqlPageQuery.setQuerySql(querySql.toString());
		return this.findDataWithPage(sqlPageQuery);
	}
}
